Lab 2 - Data Visualization and Publication¶

This is my demonstration of what I learnt during week 2 on the different ways and packages that we can visuailze data in python.

Daniel Beckley - 8846774

For the purpose of this demonstration, I will be using the same data for each of the graphs below, just to properly understand how the syntax for each of them work.

As I believe this will make me understand how each graph works and their peculiarities because this will guide my decision to know which graphs to use on future projects.

The graphs will also just be a line graph.

The graphs will be illustrating how much time I spent programming in the last week.

In [84]:
x_data = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
y_data = [2, 8, 7.5, 5, 7.9, 6, 3]

Graph 1 - Matplotlib¶

You can learn more here - Matplotlib Documentation

In [85]:
# Import the necessary libraries
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
In [86]:
fig, ax = plt.subplots()  # Create a figure containing a single axes.
fig.suptitle('My first visualization in matplotlib')

# this is where we pass the data we want to plot
ax.plot(x_data, y_data, marker='o')  # Plot some data on the axes.

#we want the y axis to go from 0 to 12
ax.set_ylim(0, 12)

# setting the label for the x and y axis.
ax.set_xlabel('days of the week')
ax.set_ylabel('number of hours programming')
Out[86]:
Text(0, 0.5, 'number of hours programming')

Graph 2 - Plotly¶

Learn more about plotly in its documentation

Importing the necessary libraries for plotly

In [87]:
import plotly.express as px

import plotly as plotly

the line below is specifically to make sure our plotly graph is still interactive after converting to html

In [88]:
plotly.offline.init_notebook_mode()

In plotly, this is how we can create the data for our graph

In [89]:
graph_data = {"days_of_week":x_data, "hour_per_day":y_data}

This is how we pass the data to plot in plotly, we also tell it what the x axis and y axis should be based on "columns" in our data

In [90]:
figure = px.line(graph_data, x="days_of_week", y="hour_per_day")
figure.show()

Graph 3 - Seaborn¶

Find its documentation here

In [91]:
# we install the library we need for seaborn
import seaborn as sns
sns.set_theme(style="darkgrid")
In [92]:
# seaborn works very similar to plotly in terms of telling it what data we want to plot
data_plot = {"days_of_week":x_data, "hour_per_day":y_data}
sns.lineplot(data=data_plot, x="days_of_week", y="hour_per_day")
Out[92]:
<Axes: xlabel='days_of_week', ylabel='hour_per_day'>

In addition, we can create some very complex graphs with these packages, some of which inlcude¶

A Time Series Histogram with Matplotlib¶

A time series histogram with Matplotlib

A 3D Graph in Plotly¶

A 3D Graph in Plotly

A Violin Plot in Seaborn¶

A Violin Plot in Seaborn

I have enjoyed learning on this lab assignemnt. The table below shows all the labs we will have to do in this class this semester.¶

Lab Title
Lab 1 Git and Github
Lab 2 Data Visualization and Publication
Lab 3 Univariate Linear Regression
Lab 4 Multivariate Linear and Polynomial Regression
Lab 5 Cross Validation
Lab 6 Logistic Regression
Lab 7 Performance Metrics for Classification
Lab 8 Dot Product and Matrix Multiplication in PyTorch and Tensorflow